package com.hr.minio;


import com.hr.common.utils.MinioUtil;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@RestController
@Api(tags = "文件操作")
@RequiredArgsConstructor
@CrossOrigin
@RequestMapping("file")
public class FileUploadController {

    @Value("${minio.bucket}")
    public String bucket;

    private final MinioUtil minioUtil;
    @PostMapping("upload")
    public String upload1(@RequestPart MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        if(!file.isEmpty()){
            //上传

            //返回  文件的访问地址
            return minioUtil.putObject(bucket,
                    file.getOriginalFilename(),//对象名
                    file.getInputStream());
        }
        return "fail";
    }
}
